package com.lx.tanks;

import java.awt.*;
import java.awt.event.*;

/**
 * Created by lx on 2017/4/7.
 */
public class TanksFrame extends Frame{
    private int x = 50;
    private int y = 50;
    private int high = 30;
    private int wide = 30;

    private static final  int windowHigh=800;
    private static final  int windowWide=600;

    public  Dir dir = Dir.DOWN;
    //是否移动
    private boolean isMoving = false;

    /**
     * 初始化一个窗口
     *
     */
    public  TanksFrame(){
        /**
         * 是否可见   窗口大小   窗口大小是否调整
         */
        this.setVisible(true);
        this.setSize(windowHigh,windowWide);
        this.setResizable(false);
        /**
         * 添加监听器
         * 键盘事件
         */
        this.addKeyListener(new MyKeyAdapter());
        /**
         * 添加监听器
         * 窗口关闭事件
         */
        this.addWindowListener(new MyWindowAdapter());
    }

    /**
     * jvm 会调用该方法
     * @param g 是一支画笔
     *
     */
    @Override
    public void paint(Graphics g) {
        g.fillRect(x,y,high,wide);

        if(!isMoving){
            return;
        }
        switch (dir){
            case DOWN:
                y++;
                break;
            case UP:
                y--;
                break;
            case LEFT:
                x--;
                break;
            case RIGHT:
                x++;
                break;
        }
    }

        /**
         * 监听器对象
         * 监听键盘
         * KeyAdapter 已经实现了KeyListener方法   但只是空方法
         * 使用KeyAdapter 就是应为不用去重写所有的方法
         */
    class  MyKeyAdapter extends KeyAdapter{
        @Override
        public void keyPressed(KeyEvent e) {
            int code = e.getKeyCode();
            switch (code){
                case  KeyEvent.VK_UP:
                    dir = Dir.UP;
                    isMoving = true;
                    break;
                case  KeyEvent.VK_LEFT:
                    dir = Dir.LEFT;
                    isMoving = true;
                    break;
                case  KeyEvent.VK_RIGHT:
                    dir = Dir.RIGHT;
                    isMoving = true;
                    break;
                case  KeyEvent.VK_DOWN:
                    dir = Dir.DOWN;
                    isMoving = true;
                    break;
            }
            //可能出现事件响应不均匀 改为每重画一次去修改一次坐标
/*            if(code == KeyEvent.VK_UP){
                 y --;
            }
            if(code == KeyEvent.VK_DOWN){
                y ++;
            }*/

        }

            @Override
            public void keyReleased(KeyEvent e) {
                int code = e.getKeyCode();
                switch (code){
                    case KeyEvent.VK_UP :
                        isMoving = false;
                        break;
                    case KeyEvent.VK_DOWN :
                        isMoving = false;
                        break;
                    case KeyEvent.VK_LEFT :
                        isMoving = false;
                        break;
                    case KeyEvent.VK_RIGHT :
                        isMoving = false;
                        break;
                }

            }
        }

    /**
     * 监听窗口关闭事件
     */

    class MyWindowAdapter extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent e) {
            TanksFrame.this.setVisible(false);
            //程序正常关闭 -1 异常
            System.exit(0);
        }
    }
}

